﻿/******************************************************************************
 *  All Rights Reserved , Copyright (C) 2012 , EricHu. 
 *  作    者： EricHu
 *  创建时间： 2012-3-30 13:02:31
 ******************************************************************************/

using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.ServiceModel;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.IService;
	using RDIFramework.Utilities;
	

	/// <summary>
    /// OrganizeService
	/// 服务层
	/// 
	/// 修改记录
	/// 
	///		2012-03-02 版本：1.0 EricHu 建立。
	///		
	/// 版本：1.0
	///
	/// <author>
	///		<name>EricHu</name>
	///		<date>2012-03-02</date>
	/// </author> 
	/// </summary>
	[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
	public class OrganizeService : System.MarshalByRefObject, IOrganizeService
	{
        private string serviceName = RDIFrameworkMessage.OrganizeService;
		
		/// <summary>
		/// .NET快速开发整合框架（RDIFramework.NET）数据库连接
		/// </summary>
        private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;


		/// <summary>
        /// 新增实体 
		/// </summary>
		/// <param name="userInfo">用户</param>
        /// <param name="entity">实体</param>
        /// <param name="statusCode">返回状态码信息</param>
		/// <param name="statusMessage">返回狀態訊息</param>
		/// <returns>主鍵</returns>
        public int Add(UserInfo userInfo, PiOrganizeEntity entity,out string statusCode, out string statusMessage)
		{
            int returnValue = 0;
            statusMessage = string.Empty;
            
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager manager = new PiOrganizeManager(dbProvider, userInfo);
                    returnValue = manager.AddEntity(entity, out statusCode);
                    statusMessage = manager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_Add, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
			
			return returnValue;
		}

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="entity">实体</param>
        /// <param name="statusMessage">返回狀態訊息</param>
        /// <returns>影响的行数</returns>
        public int Update(UserInfo userInfo, PiOrganizeEntity entity, out string statusMessage)
        {
            int returnValue = 0;
            statusMessage = string.Empty;
            string statusCode = string.Empty;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager manager = new PiOrganizeManager(dbProvider, userInfo);
                    returnValue = manager.Update(entity, out statusCode);
                    statusMessage = manager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_Update, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }

		/// <summary>
		/// 取得列表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <returns>DataTable</returns>
		public DataTable GetDT(UserInfo userInfo)
		{
			DataTable dataTable = new DataTable(PiOrganizeTable.TableName);

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 取得列表
                    PiOrganizeManager manager = new PiOrganizeManager(dbProvider, userInfo);
                    dataTable = manager.GetDT(PiOrganizeTable.FieldDeleteMark, 0, PiOrganizeTable.FieldSortCode);
                    dataTable.TableName = PiOrganizeTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_GetDT, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
			return dataTable;
		}

        /// <summary>
        /// 按父节点获取列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="parentId">父节点</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByParent(UserInfo userInfo, string parentId)
        {
            DataTable dataTable = new DataTable(PiOrganizeTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 获得组织机构列表
                    PiOrganizeManager organizeManager = new PiOrganizeManager(dbProvider, userInfo);
                    dataTable = organizeManager.GetDT(PiOrganizeTable.FieldParentId, parentId, PiOrganizeTable.FieldDeleteMark, 0, PiOrganizeTable.FieldSortCode);
                    dataTable.DefaultView.Sort = PiOrganizeTable.FieldSortCode;
                    dataTable.TableName = PiOrganizeTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_GetDTByParent, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return dataTable;
        }

		/// <summary>
		/// 取得实体
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="id">主鍵</param>
		/// <returns>实体</returns>
		public PiOrganizeEntity GetEntity(UserInfo userInfo, string id)
		{
			PiOrganizeEntity entity = null;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager manager = new PiOrganizeManager(dbProvider, userInfo);
                    entity = manager.GetEntity(id);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_GetEntity, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

			return entity;
		}

		
		/// <summary>
		/// 依主键数组獲取資料列表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主鍵</param>
		/// <returns>DataTable</returns>
		public DataTable GetDTByIds(UserInfo userInfo, string[] ids)
		{
			DataTable dataTable = new DataTable(PiOrganizeTable.TableName);

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager manager = new PiOrganizeManager(dbProvider, userInfo);
                    dataTable = manager.GetDT(PiOrganizeTable.FieldId, ids, PiOrganizeTable.FieldSortCode);
                    dataTable.TableName = PiOrganizeTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_GetDTByIds, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

			return dataTable;
		}
		
		/// <summary>
		/// 依据相应条件获取数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="names">字段</param>
		/// <param name="values">值</param>
		/// <returns>DataTable</returns>
		public DataTable GetDTByValues(UserInfo userInfo, string[] names, object[] values)
		{
			DataTable dataTable = new DataTable(PiOrganizeTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager manager = new PiOrganizeManager(dbProvider, userInfo);
                    dataTable = manager.GetDT(names, values);
                    dataTable.TableName = PiOrganizeTable.TableName;
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
			return dataTable;
		}
		
		/// <summary>
		/// 批量保存数据
		/// </summary>
		/// <param name="userInfo">用户</param>
        /// <param name="dataTable">数据表</param>
		/// <returns>影响的行数</returns>
        public int BatchSave(UserInfo userInfo, DataTable dataTable)
		{
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager organizeManager = new PiOrganizeManager(dbProvider, userInfo);
                    returnValue = organizeManager.BatchSave(dataTable);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_BatchSave, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
		}
		
		/// <summary>
		/// 刪除数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="id">主鍵</param>
		/// <returns>DataTable</returns>
		public int Delete(UserInfo userInfo, string id)
		{	
			int returnValue = 0;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager manager = new PiOrganizeManager(dbProvider, userInfo);
                    returnValue = manager.Delete(id);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
			return returnValue;
		}
		
		/// <summary>
		/// 批次刪除資料
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主键数组</param>
		/// <returns>影响的行数</returns>
		public int BatchDelete(UserInfo userInfo, string[] ids)
		{
			int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 开始事务
                    dbProvider.BeginTransaction();
                    PiOrganizeManager manager = new PiOrganizeManager(dbProvider, userInfo);
                    returnValue = manager.Delete(ids);
                    // 提交事务
                    dbProvider.CommitTransaction();
                }
                catch (Exception ex)
                {
                    // 撤銷資料庫事務
                    dbProvider.RollbackTransaction();
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
			return returnValue;
		}

		/// <summary>
		/// 批次設置刪除標誌
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主键数组</param>
		/// <returns>影响的行数</returns>
		public int SetDeleted(UserInfo userInfo, string[] ids)
		{
			int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager manager = new PiOrganizeManager(dbProvider, userInfo);
                    returnValue = manager.SetDeleted(ids);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_SetDeleted, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
			return returnValue;
		}


        /// <summary>
        /// 移动数据
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="organizeId">组织机构</param>
        /// <param name="parentId">父主键</param>
        /// <returns>影响行数</returns>
        public int MoveTo(UserInfo userInfo, string organizeId, string parentId)
        {          
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager organizeManager = new PiOrganizeManager(dbProvider, userInfo);
                    returnValue = organizeManager.MoveTo(organizeId, parentId);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_MoveTo, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 批量移动数据
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="organizeIds">主键数组</param>
        /// <param name="parentId">父节点主键</param>
        /// <returns>影响行数</returns>
        public int BatchMoveTo(UserInfo userInfo, string[] organizeIds, string parentId)
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiOrganizeManager organizeManager = new PiOrganizeManager(dbProvider, userInfo);
                    for (int i = 0; i < organizeIds.Length; i++)
                    {
                        returnValue += organizeManager.MoveTo(organizeIds[i], parentId);
                    }
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.OrganizeService_BatchMoveTo, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }       
	}
}
